package org.gg.boom.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.gg.boom.common.controller.BaseController;
import org.gg.boom.common.model.Response;
import org.gg.boom.common.utils.poi.ExcelUtil;
import org.gg.boom.kernel.annotation.Log;
import org.gg.boom.kernel.enums.BusinessType;
import org.gg.boom.kernel.web.service.SysPasswordService;
import org.gg.boom.modules.sys.model.param.SysLoginInfoParam;
import org.gg.boom.modules.sys.model.vo.SysLoginInfoResult;
import org.gg.boom.modules.sys.service.SysLoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统访问记录
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Autowired
    private SysLoginInfoService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public Response list(SysLoginInfoParam param) {
        Page<SysLoginInfoResult> page = logininforService.selectLogininforList(param);
        return getDataTable(page);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfoParam param) {
        Page<SysLoginInfoResult> list = logininforService.selectLogininforList(param);
        ExcelUtil<SysLoginInfoResult> util = new ExcelUtil<>(SysLoginInfoResult.class);
        util.exportExcel(response, list.getRecords(), "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public Response remove(@PathVariable Long[] infoIds) {
        return toResponse(logininforService.deleteLogininforByIds(infoIds));
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Response clean() {
        logininforService.cleanLogininfor();
        return success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public Response unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
